package com.easemob.helpdesk.activity;

import android.content.Intent;
import android.icu.text.UnicodeSetSpanner;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.helpdesk.bean.DiscountCouponDetailInfo;
import com.housekeeperapp.R;
import com.net.ApiService;
import com.net.HKHttpUtils;
import com.net.HKResponseResultCallBack;


import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * Created by xiaolong.wei on 2018/10/18.
 */

public class ScanQRCodeResultActivity extends BaseActivityV2 {
    private static final String TAG = "ScanQRCodeResultActivit";

    TextView mDiscountSimpleInstrument;
    LinearLayout mSuccessLayout;
    LinearLayout mFailedLayout;
    TextView mLostDiscountCouponTips;
    ProgressBar mLoading;
    ApiService apiService;
    DiscountCouponDetailInfo discountCouponDetailInfo;
    boolean canClick;

    @Override
    protected void initFindView() {
        mLoading=findViewById(R.id.loading);
        mLostDiscountCouponTips=findViewById(R.id.lost_discount_coupon_tips);
        mFailedLayout=findViewById(R.id.failed_layout);

        mSuccessLayout=findViewById(R.id.success_layout);

        mDiscountSimpleInstrument=findViewById(R.id.discount_simple_instrument);
        findViewById(R.id.back).setOnClickListener(view -> finish());
        findViewById(R.id.go_back).setOnClickListener(view -> finish());
        findViewById(R.id.cancel_use).setOnClickListener(view -> finish());

        findViewById(R.id.confirm_use).setOnClickListener(view -> {

            if (!canClick){
                return;
            }
            canClick = false;
            if (discountCouponDetailInfo == null){
                Toast.makeText(this,"优惠券信息异常，请重新扫码",Toast.LENGTH_LONG).show();
                finish();
                return;
            }
            useDiscountCoupon(discountCouponDetailInfo.getQrCode());
        });




    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scan_qr_code_result_activity);
        initFindView();
        canClick = true;
        Intent intent = getIntent();
        String qrContent = intent.getStringExtra("DISCOUNT_COUPON");
        getDisCountCouponInfo(qrContent);


    }

    private void getDisCountCouponInfo(String qrContent){
        apiService = HKHttpUtils.getApiService();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),qrContent);
        apiService.getDiscountCouponInfo(MainActivity.ticket,requestBody).enqueue(new HKResponseResultCallBack<DiscountCouponDetailInfo>() {
            @Override
            public void onSuccess(DiscountCouponDetailInfo discountCouponDetailInfo) {
                ScanQRCodeResultActivity.this.discountCouponDetailInfo = discountCouponDetailInfo;
                int i = 1;
                mDiscountSimpleInstrument.setText(Html.fromHtml("<font>"+discountCouponDetailInfo.getGoodsName()+"<font color='#ED5600'>"+i+"</font>张</font>"));
                changeLayout(true);
                Log.d(TAG, "onSuccess: "+discountCouponDetailInfo.getId());
            }

            @Override
            public void onGetDataFailed(int cd, String msg) {
                mLostDiscountCouponTips.setText(msg);
                changeLayout(false);
                Log.d(TAG, "onGetDataFailed: "+msg+"---"+cd);
            }

            @Override
            public void onFailed(Call call, Throwable t) {
                finish();
                Toast.makeText(ScanQRCodeResultActivity.this,"暂无可用网络，请检查后重试",Toast.LENGTH_LONG).show();
//                changeLayout(false);
//                Log.d(TAG, "onFailed: "+t.getMessage());
            }
        });
    }

    private void useDiscountCoupon(String qrCode){
        if (TextUtils.isEmpty(qrCode)){
            Toast.makeText(this,"优惠券信息验证失败",Toast.LENGTH_LONG).show();
            return;
        }

        if (apiService == null){
            apiService = HKHttpUtils.getApiService();
        }
        Log.d(TAG, "useDiscountCoupon: "+qrCode);
        apiService.useDiscouneCoupon(MainActivity.ticket,qrCode).enqueue(new HKResponseResultCallBack<Object>() {

            @Override
            public void onSuccess(Object o) {
                Toast.makeText(ScanQRCodeResultActivity.this,"使用成功",Toast.LENGTH_LONG).show();
                finish();

            }

            @Override
            public void onGetDataFailed(int cd, String msg) {
                Toast.makeText(ScanQRCodeResultActivity.this,msg,Toast.LENGTH_LONG).show();
                canClick = true;

            }

            @Override
            public void onFailed(Call call, Throwable t) {
                Toast.makeText(ScanQRCodeResultActivity.this,"暂无可用网络，请检查后重试",Toast.LENGTH_LONG).show();
                canClick = true;
            }
        });


    }

    private void changeLayout(boolean isSuccess){
        mLoading.setVisibility(View.GONE);
        if (isSuccess){
            mFailedLayout.setVisibility(View.GONE);
            mSuccessLayout.setVisibility(View.VISIBLE);
        }else {
            mSuccessLayout.setVisibility(View.GONE);
            mFailedLayout.setVisibility(View.VISIBLE);
        }
    }


/*    @OnClick({R.id.back, R.id.confirm_use, R.id.cancel_use,R.id.go_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
            case R.id.go_back:
            case R.id.cancel_use:
                finish();
                break;
            case R.id.confirm_use:
                if (!canClick){
                    return;
                }
                canClick = false;
                if (discountCouponDetailInfo == null){
                    Toast.makeText(this,"优惠券信息异常，请重新扫码",Toast.LENGTH_LONG).show();
                    finish();
                    return;
                }
                useDiscountCoupon(discountCouponDetailInfo.getQrCode());
                break;
            default:
                break;
        }
    }*/
}
